//
//  B_ActivityListView.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/11.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_ActivityListView.h"
#import "B_HomeSelectView.h"
#import "B_ActivityListTVC.h"
#import "B_ActivityPictureModel.h"

@interface B_ActivityListView()
/// 选则的view
@property (nonatomic, strong) B_HomeSelectView * searchView;


@end

@implementation B_ActivityListView


- (instancetype)init {
    if (self = [super init]) {
        _selectedIndex = 1;
        self.attentionArray = [NSMutableArray array];
        self.newsArray = [NSMutableArray array];
        self.hottestArray = [NSMutableArray array];
        self.backgroundColor = [UIColor color_main_background_color];
        [self addSubview:self.searchView];
        [self addSubview:self.tableView];
        [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo (self.mas_top).offset(5);
            make.left.equalTo (self.mas_left);
            make.right.equalTo (self.mas_right);
            make.height.equalTo (@(40));
        }];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.searchView.mas_bottom).offset(1);
            make.left.equalTo(self.mas_left).offset(0);
            make.right.equalTo(self.mas_right).offset(0);
            make.bottom.equalTo(self.mas_bottom).offset(0);
        }];
    }
    return self;
}


/**
 TODO:分段的点击的view

 @return 返回
 */
- (B_HomeSelectView *)searchView {
    if (!_searchView) {
        WeakSelf(weSelf, self);
        _searchView = [[B_HomeSelectView alloc] initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, 40)];
        _searchView.titleArray = @[@"关注", @"最新", @"距离"];
        _searchView.tapActionWithIndex = ^(NSInteger index) {
            /// TODO:执行相应的UI切换  数据刷新
            weSelf.selectedIndex = index;
            if (weSelf.tapAction) {
                weSelf.tapAction(index);
                [weSelf.tableView.tableView reloadData];
            }
        };
        
    }
    return _searchView;
}

- (BaseTableView *)tableView {
    if (!_tableView) {
        WeakSelf(weSelf, self);
        _tableView = [[BaseTableView alloc] init];
        _tableView.backgroundColor = [UIColor color_main_background_color];
        [_tableView setTableViewFrameWithTop:0 left:0 right:0 bottom:0];
        [_tableView.tableView registerNib:[UINib nibWithNibName:@"B_ActivityListTVC" bundle:nil] forCellReuseIdentifier:@"B_ActivityListTVC"];
        /// 组数
        _tableView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            if (weSelf.selectedIndex == 0) {
                return weSelf.attentionArray.count;
            } else if (weSelf.selectedIndex == 1) {
                return weSelf.newsArray.count;
            } else if (weSelf.selectedIndex == 2) {
                return weSelf.hottestArray.count;
            }
            return 0;
        };
        /// 组里的个数
        _tableView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return 1;
        };
        _tableView.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
            view.backgroundColor = [UIColor color_main_background_color];
            return view;
        };
        _tableView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
            view.backgroundColor = [UIColor color_main_background_color];
            return view;
        };
        _tableView.heightForFooterBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            return 10;
        };
        _tableView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 250;
        };
        _tableView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_ActivityPictureModel * model;
            ///TODO:在这里取模型赋值
            if (weSelf.selectedIndex == 0) {
                model = weSelf.attentionArray[indexPath.section];
            } else if (weSelf.selectedIndex == 1) {
                model = weSelf.newsArray[indexPath.section];
            } else if (weSelf.selectedIndex == 2) {
                model = weSelf.hottestArray[indexPath.section];
            }
            B_ActivityListTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_ActivityListTVC" forIndexPath:indexPath];
            [cell.B_imageView sd_setImageWithURL:[NSURL URLWithString:model.activityCover]];
            cell.B_titleLabel.text = model.activityTitle;
            cell.B_timeLabel.text = [NSObject getTime:model.startTime];
            cell.B_addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@  %@m", safeValue3(model.province), safeValue3(model.city), safeValue3(model.district), safeValue3(model.location), safeValue1(model.juli)];
            return cell;
        };
    }
    return _tableView;
}


@end
